package com.gis.service.impl;

import java.util.HashMap;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gis.enums.ApiErrorCode;
import com.gis.exception.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gis.entity.User;
import com.gis.mapper.UserMapper;
import com.gis.service.UserService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 用户注册
     */
    @Override
    public HashMap<String, Object> register(String username, String password) {
        HashMap<String, Object> result = new HashMap<>();

//        // 校验 userType 是否有效
//        if (!"admin".equals(usertype) && !"user".equals(usertype)) {
//            result.put("code", 400);
//            result.put("message", "无效的用户权限类型");
//            return result;
//        }

        // 检查用户名是否已存在
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User existingUser = userMapper.selectOne(queryWrapper);

        if (existingUser != null) {
//            result.put("code", 400);
//            result.put("message", "用户名已存在");
//            return result;
            throw new ApiException(ApiErrorCode.USER_EXISTS);

        }

        // 创建新用户并插入数据库
        User newUser = new User();
        newUser.setUsername(username);
        newUser.setPassword(password); // 建议加密存储
        newUser.setUsertype("user");
        int insertResult = userMapper.insert(newUser);

        if (insertResult > 0) {
            result.put("code", 200);
            result.put("message", "注册成功");
        } else {
            result.put("code", 500);
            result.put("message", "注册失败");
        }

        return result;
    }

    /**
     * 用户登录
     */
    @Override
    public HashMap<String, Object> login(String username, String password) {
        HashMap<String, Object> result = new HashMap<>();

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user = userMapper.selectOne(queryWrapper);

        if (user == null) {
//            result.put("code", 404);
//            result.put("message", "用户不存在");
//            return result;
            throw new ApiException(ApiErrorCode.USER_NOT_FOUND);
        }

        if (!user.getPassword().equals(password)) {
//            result.put("code", 401);
//            result.put("message", "密码错误");
//            return result;
            throw new ApiException(ApiErrorCode.PASSWORD_ERROR);

        }

        result.put("code", 200);
        result.put("message", "登录成功");
        result.put("user", user);
        return result;
    }

    /**
     * 删除用户
     */
    @Transactional
    @Override
    public HashMap<String, Object> deleteUser(String username) {
        HashMap<String, Object> result = new HashMap<>();

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        int deleteResult = userMapper.delete(queryWrapper);

        if (deleteResult > 0) {
            result.put("code", 200);
            result.put("message", "删除成功");
        } else {
//            result.put("code", 404);
//            result.put("message", "用户不存在或删除失败");
            throw new ApiException(ApiErrorCode.USER_NOT_FOUND);
        }

        return result;
    }

    /**
     * 修改用户密码
     */
    @Override
    public HashMap<String, Object> updatePassword(String username, String newPassword) {
        HashMap<String, Object> result = new HashMap<>();

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user = userMapper.selectOne(queryWrapper);

        if (user == null) {
//            result.put("code", 404);
//            result.put("message", "用户不存在");
//            return result;
            throw new ApiException(ApiErrorCode.USER_NOT_FOUND);
        }

        user.setPassword(newPassword);
        int updateResult = userMapper.update(user, queryWrapper);

        if (updateResult > 0) {
            result.put("code", 200);
            result.put("message", "密码修改成功");
        } else {
            result.put("code", 500);
            result.put("message", "密码修改失败");
        }

        return result;
    }

    /**
     * 修改用户类型（权限）
     */
    @Override
    public HashMap<String, Object> updateUsertype(String username, String newUsertype) {
        HashMap<String, Object> result = new HashMap<>();

        // 校验 userType 是否有效
        if (!"admin".equals(newUsertype) && !"user".equals(newUsertype)) {
            result.put("code", 400);
            result.put("message", "无效的用户类型");
            return result;
        }

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user = userMapper.selectOne(queryWrapper);

        if (user == null) {
//            result.put("code", 404);
//            result.put("message", "用户不存在");
//            return result;
            throw new ApiException(ApiErrorCode.USER_NOT_FOUND);
        }

        user.setUsertype(newUsertype); // 更新用户类型
        int updateResult = userMapper.update(user, queryWrapper);

        if (updateResult > 0) {
            result.put("code", 200);
            result.put("message", "用户权限修改成功");
        } else {
            result.put("code", 500);
            result.put("message", "用户权限修改失败");
        }

        return result;
    }

    @Override
    public User getUserByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 管理员新增用户
     */
    @Override
    public HashMap<String, Object> addUser(String username, String password, String usertype) {
        HashMap<String, Object> result = new HashMap<>();

        // 校验 userType 是否有效
        if (!"admin".equals(usertype) && !"user".equals(usertype)) {
            result.put("code", 400);
            result.put("message", "无效的用户权限类型");
            return result;
        }

        // 检查用户名是否已存在
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User existingUser = userMapper.selectOne(queryWrapper);

        if (existingUser != null) {
//            result.put("code", 400);
//            result.put("message", "用户名已存在");
//            return result;
            throw new ApiException(ApiErrorCode.USER_EXISTS);
        }

        // 创建新用户并插入数据库
        User newUser = new User();
        newUser.setUsername(username);
        newUser.setPassword(password); // 建议加密存储
        newUser.setUsertype(usertype);
        int insertResult = userMapper.insert(newUser);

        if (insertResult > 0) {
            result.put("code", 200);
            result.put("message", "新增用户成功");
        } else {
            result.put("code", 500);
            result.put("message", "新增用户失败");
        }

        return result;
    }



}
